
//const addr = "http://localhost:80";
const wsAddr="ws://localhost:80";
import config from "./config";

const addr = config.baseUrl;

const util = {
	
  showSuccess(title, duration) {
    uni.showToast({
      icon: "success",
      title: title,
      duration: duration,
    });
  },

  showLoading(msg) {
    uni.showLoading({
      title: msg,
    });
  },

  closeLoading() {
    uni.hideLoading();
  },
  
  showMsg(title, duration) {
    uni.showToast({
      icon: "none",
      title: title,
      duration: duration,
    });
  },
  
  clearRememberMe() {
    uni.setStorageSync("rememberMe", null);
  },
  
  clearUserInfo() {
    uni.setStorageSync("userInfo", null);
    this.clearRememberMe(); 
  },
  
  backToIndex() {
    uni.reLaunch({
      url: "/pages/index/index",
    });
  },
  
  isAutoLogin() {
    const res1 = uni.getStorageSync("rememberMe");
    const res2 = uni.getStorageSync("userInfo");
	// console.log(res1)
	// console.log(res2)
    if (res1 == true && res2) {
      return true;
    }
    return false;
  },
  
  saveUserInfo(user) {
    uni.setStorageSync("userInfo", user);
  },
  
  getUserInfo() {
    return uni.getStorageSync("userInfo");
  },
  
  login(userId, password) {
    return new Promise((resolve, reject) => {
      uni.request({
        url: addr + "/user/login",
        method: "POST",
        data: JSON.stringify({ userId, password }),
        success: (res) => {
          if (res.data.code == 1) {
            this.saveUserInfo(res.data.data); 
            resolve(res.data);
          } else {
            this.showMsg(res.data.msg, 2000); 
            reject(res.data.msg);
          }
        },
        fail: () => {
          this.showMsg("请求发送失败", 2000); 
          reject(new Error("请求发送失败"));
        },
      });
    });
  },
  
  register(userId, password, auth) {
    return new Promise((resolve, reject) => {
      uni.request({
        url: addr + "/user/register",
        method: "POST",
        data: JSON.stringify({ userId, password, auth }),
        success: (res) => {
          if (res.data.code == 1) {
            this.showMsg("注册成功!", 2000); 
            resolve(res.data);
          } else {
            this.showMsg(res.data.msg, 2000); 
            reject(res.data.msg);
          }
        },
        fail: () => {
          this.showMsg("请求发送失败", 2000);
          reject("请求发送失败");
        },
      });
    });
  },
  
  sendQuestionnaireRecord(questionnaireId,userId,score){
	  return new Promise((resolve, reject) => {
	    uni.request({
	      url: addr + "/questionnaire/records",
	      method: "POST",
	      data: JSON.stringify({ questionnaireId, userId, score }),
	      success: (res) => {
	        if (res.data.code == 1) {
	          resolve(res.data);
	        } else {
	          this.showMsg(res.data.msg, 2000); 
	          reject(res.data.msg);
	        }
	      },
	      fail: () => {
	        this.showMsg("请求发送失败", 2000);
	        reject("请求发送失败");
	      },
	    });
	  });
  },
  
  getUserQuestionnaireRecord(userId){
  	  return new Promise((resolve, reject) => {
  	    uni.request({
  	      url: addr + "/questionnaire/records/user/"+userId,
  	      method: "GET",
  	      success: (res) => {
  	        if (res.data.code == 1) {
  	          resolve(res.data);
  	        } else {
  	          this.showMsg(res.data.msg, 2000); 
  	          reject(res.data.msg);
  	        }
  	      },
  	      fail: () => {
  	        this.showMsg("请求发送失败", 2000);
  	        reject("请求发送失败");
  	      },
  	    });
  	  });
  },
  
};

export default {util,addr,wsAddr}; 